<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

//订单表
class Order extends Admin
{
    public function index(){
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //订单编号
            $where['orderid'] = $post['orderid']??'';
            //开始结束时间
            $where['time'] = $post['time']??'';
            //买家
            $where['buyer'] = $post['user']??'';
            //状态
            $where['status'] = $post['status']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }

        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }

    public function view()
    {
        $get = input('get.');
        if(isset($get['battery_order'])){
            $id = $get['battery_order'];
            //当前订单数据
            $list = model($this->request->controller())->where('id','=',$id)->with(['gl','user'])->find()->toArray();
            //当前订单用户信息
            $user = model('user')->where('id','=',$list['user_id'])->find()->toArray();
            //直属上级
            $model = model('user')->field('name')->where('id','=',$user['parent_id'])->find();
            if($model){
                //直属上级
                $list['parent_id'] = $model->toArray()['name'];
                //最高上级
                $list['zg'] = pap($user,$user['parent_id'])['name'];
            }else{
                //直属上级无
                $list['parent_id'] = 0;
                $list['zg'] = 0;
            }
            //当前订单的操作记录
            $record = model('record')->where([
                ['user_id','=',$user['id']],['order_id','=',$id]
            ])->select()->toArray();
            //由当前订单产生的分佣，积分，消费等数据

            //积分
            $integral = model('integral')->where('order_id','=',$list['id'])->with('user')->select();
            if($integral){
                $integral = $integral->toArray();
                foreach ($integral as $k=>$v){
                    switch ($v['source']){
                        case 1:
                            $wb = "产品代理积分";
                            break;
                        case 2:
                            $wb = "兑换产品";
                            break;
                        case 3:
                            $wb = "兑换贷款额度";
                            break;
                        case 4:
                            $wb = "产品代理平级积分";
                            break;
                        case 5:
                            $wb = "产品代理互助积分";
                            break;
                        case 6:
                            $wb = "商家权益积分";
                            break;
                        case 7:
                            $wb = "服务中心权益积分";
                            break;
                        case 8:
                            $wb = "租赁代理积分";
                            break;
                        case 9:
                            $wb = "租赁代理平级积分";
                            break;
                        case 10:
                            $wb = "租赁代理互助积分";
                            break;
                        case 11:
                            $wb = "服务中心租赁积分";
                            break;
                        case 12:
                            $wb = "直推商家代理";
                            break;
                        case 13:
                            $wb = "直推合伙人";
                            break;
                        case 14:
                            $wb = "下级商户租赁代理业绩分成";
                            break;
                        case 15:
                            $wb = "会员权益积分";
                            break;
                        case 16:
                            $wb = "代理权益积分";
                            break;
                    }
                    $wb = $wb ?? '';
                    $integral[$k]['source'] = $wb;
                    $integral[$k]['num'] = $v['user']['name'] . '获得了' .$v['num']. $wb;
                }
            }
            if($list['status'] != 10 && $list['status'] != 50 && $list['status'] != 60){
                $money[0]['source'] = '消费金额';
                $money[0]['num'] = $list['user']['name'] . '消费了' .$list['money'] .'元';
                if($integral){
                    $integral = array_merge($integral,$money);
                }
            }
            $integral = $integral ?? [];
            $this->assign('integral',$integral);
            $this->assign('record',$record);
            $this->assign('list',$list);
        }
        return $this->fetch();
    }
    public function update()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $model = model('order')->save(['status'=>30],['id'=>$post['id']]);
            return fh('200',1,1,$model);
        }
    }

    public function setting()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if($post['qxtime']){
                $qxtime = $post['qxtime']*3600;
                $model = model('setting')->where('name','=','qxtime')->find()->toArray();
                model('setting')->save(['time'=>$qxtime],['id'=>$model['id']]);
            }
            if($post['sqtime']){
                $sqtime = $post['sqtime'] * 86400;
                $model = model('setting')->where('name','=','sqtime')->find()->toArray();
                model('setting')->save(['time'=>$sqtime],['id'=>$model['id']]);
            }
        }
        $setting = model('setting')->select()->toArray();
        $time['xs'] = $setting[0]['time'] /3600;
        $time['ts'] = $setting[1]['time'] /86400;
        $this->assign('time',$time);
        return $this->fetch();
    }
}
